import core.effect.AbstractEffect;

class MultiEffect extends AbstractEffect {

    /**
     * @public
     * @param effects {Array} (optional) Array of {AbstractEffect}
     * @constructor
     */
    this.MultiEffect = function(effects) {
        super();
        this.effects = [];
        this.counter = 0;
        if (effects) {
            for (var i = 0, n = effects.length; i < n; ++i) {
                this.add(effects[i]);
            }
        }
    };

    /**
     * @public
     * @param effect {AbstractEffect}
     */
    this.add = function (effect) {
        effect.addEffectListener(this);
        this.effects.push(effect);
    };

    /**
     * @public
     * @param i {Number} Effect index.
     * @return {AbstractEffect}
     */
    this.get = function (i) {
        return this.effects.get(i);
    };

    /**
     * @public
     * @return {Boolean}
     */
    this.isPlaying = function () {
        return (this.counter > 0);
    };

    /**
     * @public
     */
    this.play = function () {
        if (this.counter > 0) {
            return;
        }
        this.fireStarted();
        var effects = this.effects;
        for (var i = 0, n = effects.length; i < n; ++i) {
            effects[i].play();
        }
        this.counter = effects.length;
    };

    /**
     * @public
     */
    this.pause = function () {
        if (this.counter == 0) {
            return;
        }
        var effects = this.effects;
        for (var i = 0, n = effects.length; i < n; ++i) {
            effects[i].pause();
        }
        this.firePaused();
    };

    /**
     * @public
     */
    this.resume = function () {
        if (this.counter == 0) {
            return;
        }
        var effects = this.effects;
        for (var i = 0, n = effects.length; i < n; ++i) {
            effects[i].resume();
        }
        this.fireResumed();
    };

    /**
     * @public
     */
    this.stop = function () {
        var effects = this.effects;
        for (var i = 0, n = effects.length; i < n; ++i) {
            effects[i].stop();
        }
        this.fireStopped();
    };

    /**
     * Calls when one of the effects in sequence has finished.
     */
    this.onEffectFinished = function () {
        if (--this.counter == 0) {
            this.fireFinished();
        }
    };
}